import App from "./App";

// #ifndef VUE3
import Vue from "vue";
Vue.config.productionTip = false;
App.mpType = "app";
const app = new Vue({
	...App,
});

//注册uView组件
import uView from "uview-ui";
Vue.use(uView);

app.$mount();
// #endif

// #ifdef VUE3
import {
	createSSRApp
} from "vue";
export function createApp() {
	const app = createSSRApp(App);
	return {
		app,
	};
}
// #endif

///hxds-driver
let baseUrl = "https://jd.zdwkj.com/bff-driver";

//小程序的appid和appsecret
//智爱未来-研学
// Vue.prototype.APP_ID = "wx74701d34a1619e9d";
// Vue.prototype.APP_SECRET = "688c36f49bf85e61523c6a506d72c776";


//爱学才会赢
Vue.prototype.APP_ID = "wx4ca96267cd36b3d0";
Vue.prototype.APP_SECRET = "45edcb1a0692156f5e4391cecdcc1800";

Vue.prototype.url = {
	parsePhoneNumber: `${baseUrl}/driver/obtainUserTel`,
	registerNewDriver: `${baseUrl}/driver/registerNewDriver`,
	updateDriverAuth: `${baseUrl}/driver/updateDriverAuth`,
	uploadCosPrivateFile: `${baseUrl}/cos/uploadCosPrivateFile`,
	deleteCosPrivateFile: `${baseUrl}/cos/deleteCosPrivateFile`,
	createDriverFaceModel: `${baseUrl}/driver/createDriverFaceModel`,
	verificateDriverFace: `${baseUrl}/driver/recognition/verificateDriverFace`,
	login: `${baseUrl}/driver/login`,
	logout: `${baseUrl}/driver/logout`,
	searchDriverBaseInfo: `${baseUrl}/driver/searchDriverBaseInfo`,
	searchWorkbenchData: `${baseUrl}/driver/searchWorkbenchData`,
	searchDriverAuth: `${baseUrl}/driver/searchDriverAuth`,
	startWork: `${baseUrl}/driver/startWork`,
	stopWork: `${baseUrl}/driver/stopWork`,
	receiveNewOrderMessage: `${baseUrl}/message/order/new/receiveNewOrderMessage`,
	acceptNewOrder: `${baseUrl}/order/acceptNewOrder`,
	searchDriverExecuteOrder: `${baseUrl}/order/searchDriverExecuteOrder`,
	searchDriverCurrentOrder: `${baseUrl}/order/searchDriverCurrentOrder`,
	searchOrderForMoveById: `${baseUrl}/order/searchOrderForMoveById`,
	arriveStartPlace: `${baseUrl}/order/arriveStartPlace`,
	startDriving: `${baseUrl}/order/startDriving`,
	uploadRecordFile: `${baseUrl}/monitoring/uploadRecordFile`,
	updateOrderStatus: `${baseUrl}/order/updateOrderStatus`,
	modifyOrderStatus: `${baseUrl}/order/modifyOrderStatus`,
	updateBillFee: `${baseUrl}/order/updateBillFee`,
	searchReviewDriverOrderBill: `${baseUrl}/order/searchReviewDriverOrderBill`,
	searchOrderStatus: `${baseUrl}/order/searchOrderStatus`,
	updateOrderAboutPayment: `${baseUrl}/order/updateOrderAboutPayment`,
	searchDriverOrderByPage: `${baseUrl}/order/searchDriverOrderByPage`,
	searchOrderById: `${baseUrl}/order/searchOrderById`,
	startCommentWorkflow: `${baseUrl}/comment/startCommentWorkflow`,
	create_order: `${baseUrl}/order/createNewOrder`,
	// modifyOrderStatus:`${baseUrl}/order/modifyOrderStatus`,
	updateOrderLocationCache:`${baseUrl}/driver/location/updateOrderLocationCache`,
};

Vue.prototype.tencent = {
	map: {
		referer: "嘿代驾",
		// key: "WSMBZ-VSAKM-Q2R64-6UTJ6-XKURF-SBBRM",
		key: "BDHBZ-AUI6Q-M475G-BS5C3-U3UCF-VNFON",
	},
};

Vue.prototype.ajax = function(url, method, data, fun, load) {
	let timer = null;
	if (load == true || load == undefined) {
		uni.showLoading({
			title: "加载中",
		});
		timer = setTimeout(function() {
			uni.hideLoading();
		}, 60 * 1000);
	}

	uni.request({
		url: url,
		method: method,
		header: {
			token: uni.getStorageSync("token"),
		},
		data: data,
		success: function(resp) {
			// console.log(resp)
			if (load == true || load == undefined) {
				clearTimeout(timer);
				uni.hideLoading();
			}
			if (resp.statusCode == 401) {
				uni.redirectTo({
					url: "/pages/login/login.vue",
				});
			} else if (resp.statusCode == 200 && resp.data.code == 200) {
				let data = resp.data;
				if (data.hasOwnProperty("token")) {
					let token = data.token;
					uni.setStorageSync("token", token);
				}
				fun(resp);
			} else if (resp.data.error == "该微信无法注册") {
				uni.showToast({
					icon: "none",
					title: "该微信无法注册",
				});
			} else if (resp.data.error == "当前手机号与注册手机号不一致") {
				uni.showToast({
					icon: "none",
					title: "当前手机号与注册手机号不一致",
				});
			} else {
				uni.showToast({
					icon: "none",
					title: "执行异常",
				});
				console.error(resp.data);
			}
		},
		fail: function(error) {
			if (load == true || load == undefined) {
				clearTimeout(timer);
				uni.hideLoading();
			}
		},
	});
};

Vue.prototype.refreshMessage = function(that) {
	uni.request({
		url: that.url.refreshMessage,
		method: "POST",
		header: {
			token: uni.getStorageSync("token"),
		},
		data: {
			identity: "driver",
		},
		success: function(resp) {
			if (resp.statusCode == 401) {
				uni.redirectTo({
					url: "/pages/login/login.vue",
				});
			} else if (resp.statusCode == 200 && resp.data.code == 200) {
				uni.$emit("updateMessageService", true);
				let result = resp.data.result;
				let lastRows = result.lastRows;
				let unreadRows = result.unreadRows;
				if (lastRows > 0) {
					uni.$emit("showMessageTip", lastRows);
				}
			} else {
				console.error(resp.data);
				//在工作台页面触发更新消息服务状态，显示服务可用或者不可用
				uni.$emit("updateMessageService", false);
			}
			console.log("刷新消息");
		},
		fail: function(error) {
			//在工作台页面触发更新消息服务状态，显示服务可用或者不可用
			uni.$emit("updateMessageService", false);
		},
	});
};

Vue.prototype.uploadCos = function(url, path, module, fun) {
	// uni.showLoading({
	// 	title: "上传中...",
	// })
	uni.uploadFile({
		url: url,
		filePath: path,
		name: "file",
		header: {
			token: uni.getStorageSync("token"),
		},
		formData: {
			module: module,
		},
		success: (resp) => {
			// uni.hideLoading()
			let data = JSON.parse(resp.data);
			if (resp.statusCode == 401) {
				uni.redirectTo({
					url: "/pages/login/login.vue",
				});
			} else if (resp.statusCode == 200 && data.code == 200) {
				fun(resp);
			} else {
				uni.showToast({
					icon: "none",
					title: data.error,
				});
			}
		},
		fail: () => {
			uni.showToast({
				icon: "none",
				title: '上传图片失败',
			});
			// uni.hideLoading()
		}
	});
};

Vue.prototype.upload = function(url, path, data, fun) {
	uni.uploadFile({
		url: url,
		filePath: path,
		name: "file",
		header: {
			token: uni.getStorageSync("token"),
		},
		formData: data,
		success: function(resp) {
			let data = JSON.parse(resp.data);

			if (resp.statusCode == 401) {
				uni.redirectTo({
					url: "/pages/login/login.vue",
				});
			} else if (resp.statusCode == 200 && data.code == 200) {
				fun(resp);
			} else {
				uni.showToast({
					icon: "none",
					title: data.error,
				});
			}
		},
		fail: function(error) {
			console.log(error);
		},
	});
};

Vue.prototype.toPage = function(url) {
	uni.navigateTo({
		url: url,
	});
};

//判空的方法
Vue.prototype.isEmpty = (obj) => {
	if (obj == "0") {
		return false;
	} else if (
		typeof obj == "undefined" ||
		obj == null ||
		obj == "" ||
		obj == []
	) {
		return true;
	} else {
		return false;
	}
}

//吐司的方法
Vue.prototype.showToast = (message) => {
	this.$refs.uToast.show({
		title: message,
	});
}

Vue.prototype.checkNull = function(data, name) {
	if (data == null) {
		this.$refs.uToast.show({
			title: name + "不能为空",
			type: "error",
		});
		return true;
	}
	return false;
};

Vue.prototype.checkBlank = function(data, name) {
	if (data == null || data == "") {
		this.$refs.uToast.show({
			title: name + "不能为空",
			type: "error",
		});
		return true;
	}
	return false;
};

Vue.prototype.checkValidName = function(data, name) {
	if (data == null || data == "") {
		this.$refs.uToast.show({
			title: name + "不能为空",
			type: "error",
		});
		return false;
	} else if (!/^[\u4e00-\u9fa5]{2,15}$/.test(data)) {
		this.$refs.uToast.show({
			title: name + "不正确",
			type: "error",
		});
		return false;
	}
	return true;
};

Vue.prototype.testPhone = (phoneNumber) => {
	if (!/^1[0-9]{10}$/.test(phoneNumber)) {
		return false
	}
	return true
}

Vue.prototype.checkValidTel = function(data, name) {
	if (data == null || data == "") {
		this.$refs.uToast.show({
			title: name + "不能为空",
			type: "error",
		});
		return false;
	} else if (!/^1[0-9]{10}$/.test(data)) {
		this.$refs.uToast.show({
			title: name + "不正确",
			type: "error",
		});
		return false;
	}
	return true;
};
Vue.prototype.checkValidEmail = function(data, name) {
	if (data == null || data == "") {
		this.$refs.uToast.show({
			title: name + "不能为空",
			type: "error",
		});
		return false;
	} else if (
		!/^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/.test(data)
	) {
		this.$refs.uToast.show({
			title: name + "不正确",
			type: "error",
		});
		return false;
	}
	return true;
};

Vue.prototype.checkValidAddress = function(data, name) {
	if (data == null || data == "") {
		this.$refs.uToast.show({
			title: name + "不能为空",
			type: "error",
		});
		return false;
	} else if (!/^[0-9a-zA-Z\u4e00-\u9fa5\-]{6,50}$/.test(data)) {
		this.$refs.uToast.show({
			title: name + "不正确",
			type: "error",
		});
		return false;
	}
	return true;
};

Vue.prototype.checkValidFee = function(data, name) {
	if (data == null || data == "") {
		this.$refs.uToast.show({
			title: name + "不能为空",
			type: "error",
		});
		return false;
	} else if (!/^[1-9]\d*\.\d{1,2}$|^0\.\d{1,2}$|^[1-9]\d*$/.test(data)) {
		this.$refs.uToast.show({
			title: name + "不正确",
			type: "error",
		});
		return false;
	}
	return true;
};

Vue.prototype.changeNumber = function(value) {
	let newValue = ["", ""];
	let fr = 1000;
	const ad = 1;
	let num = 3;
	const fm = 1;
	while (value / fr >= 1) {
		fr *= 10;
		num += 1;
	}
	if (num <= 4) {
		// 千
		newValue[1] = "千";
		newValue[0] = parseInt(value / 1000) + "";
	} else if (num <= 8) {
		// 万
		const text1 = parseInt(num - 4) / 3 > 1 ? "千万" : "万";
		const fm = "万" === text1 ? 10000 : 10000000;
		newValue[1] = text1;
		newValue[0] = value / fm + "";
	} else if (num <= 16) {
		// 亿
		let text1 = (num - 8) / 3 > 1 ? "千亿" : "亿";
		text1 = (num - 8) / 4 > 1 ? "万亿" : text1;
		text1 = (num - 8) / 7 > 1 ? "千万亿" : text1;
		// tslint:disable-next-line:no-shadowed-variable
		let fm = 1;
		if ("亿" === text1) {
			fm = 100000000;
		} else if ("千亿" === text1) {
			fm = 100000000000;
		} else if ("万亿" === text1) {
			fm = 1000000000000;
		} else if ("千万亿" === text1) {
			fm = 1000000000000000;
		}
		newValue[1] = text1;
		newValue[0] = parseInt(value / fm) + "";
	}
	if (value < 1000) {
		newValue[1] = "";
		newValue[0] = value + "";
	}
	let temp = Math.floor(newValue[0] * 100) / 100;
	return temp + newValue[1];
};
